package com.lyu.travel.controller;

import com.lyu.travel.core.entity.Place;
import com.lyu.travel.core.entity.Province;
import com.lyu.travel.core.entity.User;
import com.lyu.travel.core.service.PlaceService;
import com.lyu.travel.core.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: [travel]
 * @Package: [com.lyu.travel.controller]
 * @ClassName: [ProvinceController]
 * @Description: 省份控制类
 * @Author: [78483]
 * @CreateDate: [2020-06-13 18:01]
 * @UpdateUser: [78483]
 * @UpdateDate: [2020-06-13 18:01]
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 */
@Controller
public class ProvinceController {
    @Autowired
    ProvinceService provinceService;

    @Autowired
    PlaceService placeService;

    //省份管理
    @GetMapping("/province")
    public String province(Model model) {
        //业务处理逻辑
        model.addAttribute("provinceList11", provinceService.updatePlacecounts());
        model.addAttribute("provinceList", provinceService.findAll());
        return "provinceList";
    }

    //添加
    @PostMapping("/proSave")
    @ResponseBody
    public  Object savePro(Province province){
        //处理请求
        Map map= new HashMap<>();
        if (provinceService.selectByProvincename(province.getName())!=null){
            map.put("msg1", "省份名重复");
        }else if (province.getName()=="" || province.getTags()=="" ) {

            map.put("msg2","不能为空");
        }else{
            provinceService.insert(province);
            map.put("success",true);
            map.put("data",province);
        }
        return map  ;
    }

    //删除
    @PostMapping("/proDelete")
    @ResponseBody
    public int deletePro(int id){
        //处理请求
        return provinceService.deleteProvinceById(id);
    }

    //更新
    @PostMapping("/proUpdate")
    @ResponseBody
    public Object updatePro(Province province){
        //处理请求
        Map map= new HashMap<>();
        if (province.getName()=="" || province.getTags()=="") {
            map.put("msg2", "不能为空");
        }else{
            provinceService.updateProvince(province);
        }
        return map  ;
    }

    int a;
    //查询景点
    @PostMapping("/proSelect")
    @ResponseBody
    public List<Place> selectPro(int id){
        //处理请求
        //获得id
        a=id;
        return placeService.findPlaceByid(id);
    }

    //查询当地景点
    @GetMapping("/place1")
    public String place(Model model) {
        //业务处理逻辑
        model.addAttribute("placeList", placeService.findPlaceByid(a));
        model.addAttribute("placeList1", provinceService.findAll());
        return "placeList";
    }


}
